package com.itbour.service.trade.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.trade.CouponDao;
import com.itbour.dao.trade.InvoiceDao;
import com.itbour.dao.trade.ItemsDao;
import com.itbour.dao.trade.PrintParaDao;
import com.itbour.dto.com.IdNameDto;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.tg.OrgInfoDto;
import com.itbour.dto.trade.CouponInfoDto;
import com.itbour.dto.trade.InvoiceInfoDto;
import com.itbour.dto.trade.ItemInfoDto;
import com.itbour.dto.trade.PrintParaInfoDto;
import com.itbour.service.trade.CouponService;
import com.itbour.service.trade.ItemsService;
import com.itbour.service.trade.PrintParaService;
import com.opensymphony.xwork2.ActionContext;


/**
* 分页映射
* 
* copyright   itbour team 2016
* @author     abby
*
*/

public class CouponServiceImpl implements CouponService {

	private  CouponDao couponDao;
	
	/**
	 * @param countKey
	 *            查询该表所有记录行的SQL语句ID
	 * @param sqlKey
	 *            需要查询记录的SQL语句ID
	 * @param param
	 *            传递给查询语句的参数
	 * @param pageNo
	 *            需要查询的实际页
	 * @param pageSize
	 *            每页记录行数
	 * @return page对象
	 */
	public Page couponPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = couponDao.selectCouponCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<CouponInfoDto> data = couponDao.selectCouponInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}

	public List<CouponInfoDto> selectCouponInfo(Map<String,Object> param) {
		return couponDao.selectCouponInfo(param);
	}
	//删除优惠券
	public void delCoupon(String delIds) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		couponDao.deleteCouponInfoLogic(param);
	}
	//增加优惠券信息
	public void addCoupon(CouponInfoDto couponInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("org_id", couponInfo.getOrg_id());
		param.put("owner", couponInfo.getOwner());
		param.put("lot_no", couponInfo.getLot_no());
		param.put("type", couponInfo.getType());
		param.put("use", couponInfo.getUse());
		param.put("c_code", couponInfo.getC_code());
		param.put("limit_type", couponInfo.getLimit_type());
		param.put("discount", couponInfo.getDiscount());
		param.put("min_order_fee", couponInfo.getMin_order_fee());
		param.put("expiry_time", couponInfo.getExpiry_time());
		param.put("state", couponInfo.getState());
		if(couponInfo.getMobile()!=null) {
			param.put("mobile", couponInfo.getMobile());
		}
		param.put("desc", couponInfo.getDesc());
		param.put("update_by", couponInfo.getUpdate_by());
		couponDao.insertCouponInfo(param);
	}
	
	public void updCoupon(Map<String,Object> param) {
		couponDao.updateCouponInfoById(param);
	}
	
	public void grantCoupon(Map<String,Object> param) {
		couponDao.grantCoupon(param);
	}
	
	public void grantCouponByMobile(Map<String,Object> param) {
		couponDao.grantCouponByMobile(param);
	}
	
	public List<IdNameDto> selectLotNoInfo(Map<String,Object> param) {
		return couponDao.selectLotNoInfo(param);
	}
	
	public List<OrgInfoDto> selectOrgInfo(Map<String,Object> param) {
		return couponDao.selectOrgInfo(param);
	}
	public List<IdNameDto> selectOrgUserInfo(Map<String,Object> param) {
		return couponDao.selectOrgUserInfo(param);
	}

	public CouponDao getCouponDao() {
		return couponDao;
	}

	public void setCouponDao(CouponDao couponDao) {
		this.couponDao = couponDao;
	}

}

